package com.fruit.service.goods.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.goods.GoodsMapper;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.goods.GoodsSpec;
import com.fruit.pojo.goods.InterfaceConfig;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.pojo.goods.GoodsSpecTime;
import com.fruit.pojo.goods.GoodsSpecTimeHarvest;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.redis.JedisUtil;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

@Service
public class GoodsService implements GoodsManager {

	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private JedisUtil jedisService;
	
	
	@Override
	public void setToken(String key, String value) {
		jedisService.set(key, value);
		jedisService.expire(key, 1800);
	}

	/**
	 * 获取商品详细信息
	 * @param params
	 * @return
	 */
	@Override
	public List<GoodsSpecTime> getGoodsDetail(Integer goodsId) {
		List<Map<String, String>> unitList = goodsMapper.getUnitNameByUnit();
    	Map<String, String> unit = new HashMap<String, String>();
    	Map<String, String> unitMap = new HashMap<String, String>();
    	if(unitList!=null){
    		for(int i=0;i<unitList.size();i++){
    			unitMap = unitList.get(i);
    			unit.put((String) unitMap.get("unit"), (String) unitMap.get("name"));
        	}
    	}
		List<GoodsSpecTime> goodsSpecTimeList = goodsMapper.getGoodsDetail(goodsId);
		if(goodsSpecTimeList!=null){
			for(int i=0;i<goodsSpecTimeList.size();i++){//endUseTime（...） endTime（年月日）
				GoodsSpecTime goodsSpecTime = goodsSpecTimeList.get(i);
				goodsSpecTime.setUnitDesc(unit.get(goodsSpecTime.getUnit()));
				goodsSpecTime.setHarvestUnitDesc(unit.get(goodsSpecTime.getHarvestUnit()));
				goodsSpecTime.setEndUseTime(DateUtil.yMdhmsToYmd(goodsSpecTime.getEndUseTime()));
				boolean dateB = DateUtil.compareDate(goodsSpecTime.getEndTime(),DateUtil.getTime());
				if(dateB){//true 表示当前系统时间 小于 商品下架时间一样  （商品详情页面的 今日认养时间 为当前系统时间）
					goodsSpecTime.setTodayDate(DateUtil.yMdhmsToYmd(DateUtil.getTime()));
				}else{//false 表示当前系统时间 大于等于 商品下架时间一样  （商品详情页面的 今日认养时间 为下架时间）
					goodsSpecTime.setTodayDate(DateUtil.yMdhmsToYmd(goodsSpecTime.getEndTime()));
				}
				goodsSpecTime.setEndTime(DateUtil.yMdHmsToCh(goodsSpecTime.getEndTime()));//此日期必须放到最后 进行处理 因为前几行代码用到此未处理的日期
			}
		}
		return goodsSpecTimeList;
	}

	
	
	@Override
	public Map<String, Object> getGoodsSpecTimeById(Map<String, Object> params) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			GoodsSpecTime gst = goodsMapper.getGoodsSpecTimeById(params);
			resMap.put("gst", gst);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "查询成功!"));
			return resMap;
		}catch(Exception e){
			e.printStackTrace();
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!"));
			return resMap;
		}
	}



	/**
	 * 商品列表
	 * @param page
	 * @return
	 */
	@Override
	public List<GoodsSpec> getGoodsList(Map<String, Object> pam) {
		List<GoodsSpec> GoodsSpecList = goodsMapper.getGoodsList(pam);
		if(GoodsSpecList!=null){
			for(int i=0;i<GoodsSpecList.size();i++){
				GoodsSpec goodsSpec = GoodsSpecList.get(i);
				if(goodsSpec.getSaleType().equals("00")){
					goodsSpec.setStockNum(0);
				}
				if(goodsSpec.getStockNum()==0){//如果库存为0  那么状态设置为 售馨状态
					goodsSpec.setSaleType("00");
				}
			}
		}
		return GoodsSpecList;
	}

	@Override
	public Base getBase(Integer id) {
		// TODO Auto-generated method stub
		Base base = goodsMapper.getBase(id);
		boolean a = DateUtil.compareDateHMS(DateUtil.getHMS(), "6:00:00");//当前时间大于6点 小于20点为白天
		boolean b = DateUtil.compareDateHMS("20:00:00", DateUtil.getHMS());
		if(a && b){//如果都是 true的话 说明 当前时间大于6点小于20点 则为白天 否则为黑夜
			base.setTimeType("00");
		}else{
			base.setTimeType("01");
		}
		return base;
	}

	@Override
	public String getClassIdByPid(String classId) {
		// TODO Auto-generated method stub
		List<String> classIdList = goodsMapper.getClassIdByPid(classId);
		String classIds = null;
		if(classIdList!=null && classIdList.size()>0){
			classIds = StringUtils.join(classIdList, ",");//把list的值转换为String类型 用逗号隔开
		}
		return classIds;
	}

	@Override
	public List<Map<String, String>> getUnitNameByUnit() {
		// TODO Auto-generated method stub
		return goodsMapper.getUnitNameByUnit();
	}

	/**
	 * 预计产量信息
	 */
	@Override
	public List<GoodsSpecTimeHarvest> findGoodsSpecTimeHarvest(Integer id) {
		// TODO Auto-generated method stub
		List<GoodsSpecTimeHarvest> gsh = goodsMapper.findGoodsSpecTimeHarvest(id);
		if(gsh!=null && gsh.size()>0){
			for(int i=0;i<gsh.size();i++){
				GoodsSpecTimeHarvest gst = gsh.get(i);
				boolean isState = DateUtil.compareDate(DateUtil.getDay(),gst.getExpectHarvestTime());
    			if(isState){//如果 isState为true  说明预计收获时间大于当前系统时间 那么就 是未收获 如果是小于那么就说 已经收获啦
    				gst.setState("00");//已收获
    			}else{
    				gst.setState("01");//未收获
    			}
    			gst.setExpectHarvestTime(DateUtil.yMdToYmd(gst.getExpectHarvestTime()));
			}
		}
		return gsh;
	}

	@Override
	public List<InterfaceConfig> getInterfaceConfig(String appType) {
		return goodsMapper.getInterfaceConfig(appType);
	}



	@Override
	public List<RightConfig> getRightConfigList() {
		return goodsMapper.getRightConfigList();
	}
	

}
